package top.malegod.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.malegod.mapper.ManageMapper;
import top.malegod.model.Category;
import top.malegod.model.Comments;
import top.malegod.model.Community;
import top.malegod.model.Integral;
import top.malegod.model.Manage;
import top.malegod.model.Order;
import top.malegod.model.Rank;
import top.malegod.model.User;
import top.malegod.service.ManageService;
import top.malegod.utils.ServerResponse;
import top.malegod.utils.Utils;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/29 14:30
 * @description: 管理员实现service层接口
 */
@Service
public class ManageServiceImpl implements ManageService {

  @Autowired
  private ManageMapper manageMapper;

  /**
   * 统计数量service层操作
   */
  @Override
  public ServerResponse manageBaseCount() {
    int usersCount = manageMapper.findUsersCount();
    int ordersCount = manageMapper.findOrdersCount();
    int communityCount = manageMapper.findCommunityCount();
    int commentsCount = manageMapper.findCommentsCount();
    Map<String, Integer> map = new HashMap<>();
    map.put("usersCount",usersCount);
    map.put("ordersCount",ordersCount);
    map.put("communityCount",communityCount);
    map.put("commentsCount",commentsCount);
    return ServerResponse.createSuccess(map);
  }

  /**
   * 管理员登录service层操作
   */
  @Override
  public ServerResponse manageLogin(String username, String password) {
    int count = manageMapper.findManageCountByUsername(username);
    if (count != 1) {
      return ServerResponse.createError("管理员不存在");
    }
    String md5 = Utils.md5(password);
    Manage manage = manageMapper.findManageByUsernameAndPassword(username, md5);
    if (manage == null){
      return ServerResponse.createError("密码错误");
    }
    return ServerResponse.createSuccess(manage);
  }

  /**
   * 管理员获取所有用户信息service层操作
   */
  @Override
  public ServerResponse manageUserList(Integer pageNum, Integer pageSize) {
    Page<User> page = PageHelper.startPage(pageNum,pageSize);
    List<User> list = manageMapper.findAllUsers();
    if (list == null){
      return ServerResponse.createError("没有获取到任何用户信息");
    }
    PageInfo info = new PageInfo(list);
    return ServerResponse.createSuccess(info);
  }

  /**
   * 删除用户service层操作
   */
  @Override
  public ServerResponse manageDeleteUser(String id) {
    int count = manageMapper.deleteUser(id);
    if (count == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  @Override
  public ServerResponse managerRestoreUserList(Integer pageNum, Integer pageSize) {
    Page<User> page = PageHelper.startPage(pageNum,pageSize);
    List<User> list = manageMapper.findRestoreAllUsers();
    if (list == null){
      return ServerResponse.createError("没有获取到任何用户信息");
    }
    PageInfo info = new PageInfo(list);
    return ServerResponse.createSuccess(info);
  }

  /**
   * 用户恢复service层操作
   */
  @Override
  public ServerResponse manageRestoreUser(String id) {
    int count = manageMapper.restoreUser(id);
    if (count == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 获取订单service层操作
   */
  @Override
  public ServerResponse manageOrderList(Integer pageSize, Integer pageNum) {
    Page<Order> page = PageHelper.startPage(pageNum,pageSize);
    List<Order> list = manageMapper.findAllOrders();
    if (list == null){
      return ServerResponse.createError("没有获取到任何订单信息");
    }
    PageInfo info = new PageInfo(list);
    return ServerResponse.createSuccess(info);
  }


  /**
   * 删除订单service层操作
   */
  @Override
  public ServerResponse manageOrderDelete(String id) {
    int success = manageMapper.deleteOrder(id);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 获取社区信息service层操作
   */
  @Override
  public ServerResponse manageCommunityList(Integer pageSize, Integer pageNum) {
    Page<Community> page = PageHelper.startPage(pageNum,pageSize);
    List<Community> list = manageMapper.findCommunityList();
    if (list == null){
      return ServerResponse.createError("未获取到社区信息或社区无分享");
    }
    PageInfo info = new PageInfo(list);
    return ServerResponse.createSuccess(info);
  }

  /**
   * 社区搜索service层操作
   */
  @Override
  public ServerResponse manageCommunitySearch(Integer pageSize, Integer pageNum,
      String content) {
    Page<Community> page = PageHelper.startPage(pageNum,pageSize);
    List<Community> list = manageMapper.searchCommunityByContent(content);
    if (list == null){
      return ServerResponse.createError("未获取到社区信息或社区无分享");
    }
    PageInfo info = new PageInfo(list);
    return ServerResponse.createSuccess(info);
  }

  /**
   * 删除不正当分享service层操作
   */
  @Override
  public ServerResponse manageDeleteCommunity(String id) {
    int count = manageMapper.deleteCommunity(id);
    if (count == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }


  /**
   * 评论列表service层操作
   */
  @Override
  public ServerResponse manageCommentsList(Integer pageSize, Integer pageNum) {
    Page<Comments> page = PageHelper.startPage(pageNum,pageSize);
    List<Comments> list = manageMapper.findCommentsList();
    if (list == null){
      return ServerResponse.createError("未获取到任何评论");
    }
    PageInfo info = new PageInfo(list);
    return ServerResponse.createSuccess(info);
  }

  /**
   * 评论删除service层操作
   */
  @Override
  public ServerResponse manageCommentsDelete(String id) {
    int success = manageMapper.deleteComments(id);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 分类列表service层操作
   */
  @Override
  public ServerResponse manageCategoryList() {
    List<Category> categories = manageMapper.findCategoryList();
    if (categories == null){
      return ServerResponse.createError("未获取到任何分类");
    }
    return ServerResponse.createSuccess(categories);
  }

  /**
   * 增加分类service层操作
   */
  @Override
  public ServerResponse manageAddCategory(String name) {
    int success = manageMapper.addCategory(name);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 修改分类service层操作
   */
  @Override
  public ServerResponse manageUpdateCategory(Integer id, String name) {
    int success = manageMapper.updateCategory(id, name);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 删除分类service层操作
   */
  @Override
  public ServerResponse manageDeleteCategory(Integer id) {
    int success = manageMapper.deleteCategory(id);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 积分列表service层操作
   * @param pageNum
   * @param pageSize
   */
  @Override
  public ServerResponse manageIntegralList(Integer pageNum, Integer pageSize) {
    Page<Integral> page = PageHelper.startPage(pageNum,pageSize);
    List<Integral> list = manageMapper.findIntegralList();
    if (list == null){
      return ServerResponse.createError("未获取到任何评论");
    }
    PageInfo info = new PageInfo(list);
    return ServerResponse.createSuccess(info);
  }

  /**
   * 积分修改service层操作
   */
  @Override
  public ServerResponse manageUpdateIntegral(Double count, String userId) {
    int success = manageMapper.updateIntegral(count, userId);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 等级列表service层操作
   */
  @Override
  public ServerResponse manageRankList() {
    List<Rank> ranks = manageMapper.findRankList();
    if (ranks == null){
      return ServerResponse.createError("未获取到任何等级信息");
    }
    return ServerResponse.createSuccess(ranks);
  }

  /**
   * 等级修改service层操作
   */
  @Override
  public ServerResponse manageUpdateRank(Integer id, String name) {
    int success = manageMapper.updateRank(id, name);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 删除等级service层操作
   */
  @Override
  public ServerResponse manageDeleteRank(Integer id) {
    int success = manageMapper.deleteRank(id);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }

  /**
   * 增加等级service层操作
   */
  @Override
  public ServerResponse manageAddRank(String name) {
    int success = manageMapper.addRank(name);
    if (success == 0){
      return ServerResponse.createError("error");
    }
    return ServerResponse.createSuccess("success");
  }
}
